package com.gugee.tiktok.web.repository.impl;

import com.gugee.tiktok.web.dto.AwemeCollectResponse;
import com.gugee.tiktok.web.dto.CollectSearchData;
import com.gugee.tiktok.web.repository.AwemeCollectionExtRepository;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class AwemeCollectionExtRepositoryImpl implements AwemeCollectionExtRepository {


    public Page<AwemeCollectResponse> getPage(CollectSearchData searchData, Long userId){
        StringBuffer sqlSelect = new StringBuffer("select c.id collectId,a.o_aweme_id awemeId,a.score,a.o_uid uid," +
                "a.o_play_count viewCount,a.o_digg_count likeCount,a.o_create_time createTime, a.o_duration duration," +
                "a.o_comment_count reviewCount,a.o_share_count shareCount,a.o_desc title,a.o_cover cover,o_share_url shareUrl ");

        StringBuffer sql = new StringBuffer("from aweme_collection c " +
                "left join aweme a on c.aweme_id = a.o_aweme_id " +
                "where c.user_id = ? ");
        List<Object> params = new ArrayList<>();
        params.add(userId);

        if(!StringUtils.isEmpty(searchData.getKeyword())){
            sql.append("and a.o_desc like ? ");
            params.add("%"+searchData.getKeyword()+"%");
        }
        String sort = "score";

        switch (searchData.getSort()){
            case 1:
                sort = "score";
                break;
            case 2:
                sort = "viewCount";
                break;
            case 3:
                sort = "likeCount";
                break;
            case 4:
                sort = "reviewCount";
                break;
            case 5:
                sort = "shareCount";
                break;
            default:
                break;
        }

        sql.append("order by ").append(sort).append(" desc ");

        return JdbcTemplateHelper.paginate(AwemeCollectResponse.class,searchData.getPage(),searchData.getPageSize(),sqlSelect.toString(),sql.toString(),params.toArray());
    }
}
